package com.hualude.roadsign.controller;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.JsonMsgDto;
import com.hualude.roadsign.dto.GuardrailDto;
import com.hualude.roadsign.service.GuardrailManageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by xu_jiaHui on 2017/8/18.
 */
@Controller
@RequestMapping("/rds_guardrailController")
public class GuardrailManageController {
    @Resource
    private GuardrailManageService guardrailManageService;

    @RequestMapping("/getGuardrail4DT")
    @ResponseBody
    public DTResponseDto getGuardrail4DT(DTRequestDto dtRequestDto,
                                         String actorIds,
                                         Integer orgId,
                                         Boolean audit,
                                         String startRoad,
                                         String endRoad,
                                         String roadName) throws Exception {
        DTResponseDto dtResponseDto = new DTResponseDto();
        try {
            dtResponseDto = guardrailManageService.getGuardrail4DT(
                    dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtResponseDto;
    }

    @RequestMapping("/deleteGuardrail")
    @ResponseBody
    public JsonMsgDto deleteGuardrail(String guardrailIds) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.deleteGuardrail(guardrailIds));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/updateGuardrail")
    @ResponseBody
    public JsonMsgDto updateGuardrail(GuardrailDto guardrailDto) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.updateGuardrail(guardrailDto));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/addGuardrail")
    @ResponseBody
    public JsonMsgDto addGuardrail(GuardrailDto guardrailDto) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.addGuardrail(guardrailDto));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/auditGuardrail")
    @ResponseBody
    public JsonMsgDto auditGuardrail(String guardrailIds) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.auditGuardrail(guardrailIds));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getCoordinatesById")
    @ResponseBody
    public JsonMsgDto getCoordinatesById(Integer guardrailId, Integer type) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.getCoordinatesById(guardrailId, type));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getStartRoadByRoadName")
    @ResponseBody
    public JsonMsgDto getStartRoadByRoadName(String roadName, String actorIds, Integer orgId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(guardrailManageService.getStartRoadByRoadName(roadName, actorIds, orgId));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


    @RequestMapping("/getEndRoadByStartRoad")
    @ResponseBody
    public JsonMsgDto getEndRoadByStartRoad(String guardrailRoadName, String startRoad, String actorIds, Integer orgId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(guardrailManageService.getEndRoadByStartRoad(guardrailRoadName, startRoad, actorIds, orgId));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


    @RequestMapping("/getCity4Guardrail")
    @ResponseBody
    public JsonMsgDto getCity4Guardrail(String actorIds, Integer orgId, Integer mCount, Integer mSize) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.getCity4Guardrail(actorIds, orgId, mCount, mSize));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getStartRoadByCity")
    @ResponseBody
    public JsonMsgDto getStartRoadByCity(String city, String actorIds, Integer orgId, Integer mCount, Integer mSize) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.getStartRoadByCity(city, actorIds, orgId, mCount, mSize));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getSectionByStartRoad4App")
    @ResponseBody
    public JsonMsgDto getSectionByStartRoad4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.getSectionByStartRoad4App(city, roadName, actorIds, orgId, mCount, mSize));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setJsonData(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getGuardrail4APP")
    @ResponseBody
    public JsonMsgDto getGuardrail4APP(String actorIds,
                                       Integer orgId,
                                       Integer mCount,
                                       Integer mSize,
                                       String city,
                                       String startRoad,
                                       String endRoad,
                                       String roadName) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.getGuardrail4APP(
                    actorIds, orgId, mCount, mSize, city, startRoad, endRoad, roadName));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getGuardrailById")
    @ResponseBody
    public JsonMsgDto getGuardrailById(Integer guardrailId) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.getGuardrailById(guardrailId));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getRoadName4GuardrailByCity")
    @ResponseBody
    public JsonMsgDto getRoadName4GuardrailByCity(String actorIds, Integer orgId, String city, Integer mCount, Integer mSize) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(guardrailManageService.getRoadName4GuardrailByCity(actorIds, orgId, city, mCount, mSize));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getStartRoadByRoadName4App")
    @ResponseBody
    public JsonMsgDto getStartRoadByRoadName4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(guardrailManageService.getStartRoadByRoadName4App(city, roadName, actorIds, orgId, mCount, mSize));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getEndRoadByStartRoad4App")
    @ResponseBody
    public JsonMsgDto getEndRoadByStartRoad4App(String city, String roadName, String startRoad, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(guardrailManageService.getEndRoadByStartRoad4App(city, roadName, startRoad, actorIds, orgId, mCount, mSize));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getGuardrail4Map4App")
    @ResponseBody
    public JsonMsgDto getGuardrail4Map4App(Integer orgId,
                                           String actorIds,
                                           double neLongitude,
                                           double neLatitude,
                                           double swLongitude,
                                           double swLatitude) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(guardrailManageService.getGuardrail4Map4App
                    (orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }
}
